package com.mc.fil.service.impl;

import java.util.List;

import com.mc.common.core.domain.model.LoginUser;
import com.mc.common.utils.DateUtils;
import com.mc.common.utils.ServletUtils;
import com.mc.fil.domain.FilMultimedia;
import com.mc.fil.domain.FilSow;
import com.mc.fil.mapper.FilMultimediaMapper;
import com.mc.framework.web.service.TokenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.mc.fil.mapper.FilManureMapper;
import com.mc.fil.domain.FilManure;
import com.mc.fil.service.IFilManureService;

/**
 * 底肥信息 Service业务层处理
 *
 * @author mc
 * @date 2021-04-15
 */
@Service
public class FilManureServiceImpl implements IFilManureService
{
    @Autowired
    private FilManureMapper filManureMapper;
    @Autowired
    private FilMultimediaMapper multimediaMapper;
    @Autowired
    private TokenService tokenService;

    /**
     * 查询底肥信息
     *
     * @param id 底肥信息 ID
     * @return 底肥信息
     */
    @Override
    public FilManure selectFilManureById(Long id)
    {
        FilManure filManure = filManureMapper.selectFilManureById(id);
        FilMultimedia multimedia = new FilMultimedia();
        multimedia.setFilId(id);
        multimedia.setGenre("filManure");
        filManure.setPhotos(multimediaMapper.selectFilMultimediaList(multimedia));
        return filManure;
    }

    /**
     * 查询底肥信息 列表
     *
     * @param filManure 底肥信息
     * @return 底肥信息
     */
    @Override
    public List<FilManure> selectFilManureList(FilManure filManure)
    {
        List<FilManure> list = filManureMapper.selectFilManureList(filManure);
        for(FilManure fil : list){
            FilMultimedia multimedia = new FilMultimedia();
            multimedia.setFilId(fil.getId());
            multimedia.setGenre("filManure");
            fil.setPhotos(multimediaMapper.selectFilMultimediaList(multimedia));
        }
        return list;
    }

    /**
     * 新增底肥信息
     *
     * @param filManure 底肥信息
     * @return 结果
     */
    @Override
    public int insertFilManure(FilManure filManure)
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        filManure.setCreateTime(DateUtils.getNowDate());
        filManure.setCreateUserId(loginUser.getUsername());
        filManureMapper.insertFilManure(filManure);
        List<FilMultimedia> photos= filManure.getPhotos();
        for(FilMultimedia filMultimedia: photos){
            filMultimedia.setFilId(filManure.getId());
            filMultimedia.setCreatedBy(loginUser.getUsername());
            filMultimedia.setCreatedTime(DateUtils.getNowDate());
            filMultimedia.setGenre("filManure");
            multimediaMapper.insertFilMultimedia(filMultimedia);

        }
        return 1;
    }

    /**
     * 修改底肥信息
     *
     * @param filManure 底肥信息
     * @return 结果
     */
    @Override
    public int updateFilManure(FilManure filManure)
    {
        return filManureMapper.updateFilManure(filManure);
    }

    /**
     * 批量删除底肥信息
     *
     * @param ids 需要删除的底肥信息 ID
     * @return 结果
     */
    @Override
    public int deleteFilManureByIds(Long[] ids)
    {
        return filManureMapper.deleteFilManureByIds(ids);
    }

    /**
     * 删除底肥信息 信息
     *
     * @param id 底肥信息 ID
     * @return 结果
     */
    @Override
    public int deleteFilManureById(Long id)
    {
        return filManureMapper.deleteFilManureById(id);
    }
}